package com.shiliao.app.activity.index.ui;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import com.shiliao.app.R;
import com.shiliao.app.activity.index.presenter.SerachPresenter;
import com.shiliao.app.activity.index.view.SerachView;
import com.shiliao.app.activity.interlocution.ui.CommonProblemDetailActivity;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.MyWebView;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.ProgressWebView;
import com.yimei.framework.base.ui.BaseActivity;
import com.yimei.framework.util.LogUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yinzhiqiang on 2017/10/12.
 */

public class SearchActivity extends BaseActivity implements SerachView{

    private SerachPresenter serachPresenter;

    @BindView(R.id.index_search)
    EditText searchArticle;

    @BindView(R.id.webview)
    ProgressWebView webview;
    private MyWebView mWebView;
    @Override
    public int getLayoutId() {
        return R.layout.activity_search_text;
    }

    @Override
    public void initView() {
        WebSetUtil.setWebView(webview);
        serachPresenter=new SerachPresenter(this);
        searchArticle.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //搜索
                    String content= searchArticle.getText().toString().trim();
                    Intent intent=getIntent();
                    String fromPage=intent.getStringExtra("from");
                    if (fromPage.equals("index")) {
                        serachPresenter.requestSerach(content,YMUserManager.getUserId(SearchActivity.this));
                    }else{
                        serachPresenter.requestProble(content, YMUserManager.getUserId(SearchActivity.this));
                    }
                }
                return false;
            }
        });

//        intedata();

        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtil.e("yzq",url+"==="+YMServerUtil.DAILY_KNOW_COTURL);
                if (url.startsWith(YMServerUtil.DAILY_KNOW_COTURL)) {
//            Intent intent = new Intent(context, activity.class);
                    Intent intent = new Intent(SearchActivity.this, NewsDetailActivity.class);
                    intent.putExtra("url", url);
                    startActivity(intent);
                    return true;
                }else if(url.contains("question.php?id")){
                    Intent intent = new Intent(SearchActivity.this, CommonProblemDetailActivity.class);
                    intent.putExtra("url", url);
                    startActivity(intent);
                    return true;
                }
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });


    }

    private void  intedata(){
        Intent intent = new Intent(this, NewsDetailActivity.class);
        mWebView = new MyWebView(webview,this,YMServerUtil.DAILY_KNOW_COTURL,
                intent,null,YMServerUtil.DAILY_KNOW_REFERASHURL);
        webview.setWebViewClient(mWebView);
    }



    @OnClick(R.id.search_right)
    public void cancel(){
        this.finish();
    }

//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {//点击键盘搜索请求
//
//        if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//            LogUtil.e("yzq","------点击完成");
//           String content= searchArticle.getText().toString().trim();
//            Intent intent=getIntent();
//          String fromPage=intent.getStringExtra("from");
//            if (fromPage.equals("index")) {
//                serachPresenter.requestSerach(content);
//            }else{
//                serachPresenter.requestProble(content);
//            }
//        }
//        return super.dispatchKeyEvent(event);
//
//    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//            LogUtil.e("yzq","------点击完成");
//            String content= searchArticle.getText().toString().trim();
//            Intent intent=getIntent();
//            String fromPage=intent.getStringExtra("from");
//            if (fromPage.equals("index")) {
//                serachPresenter.requestSerach(content);
//            }else{
//                serachPresenter.requestProble(content);
//            }
//        }
//            return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void loadSerach(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webview.loadDataWithBaseURL(YMServerUtil.BASE_URL,
                        text,"text/html", "utf-8",null);
            }
        });
    }

}
